package main.java;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Set;

public class CgPoint {

	private String name;
	private boolean isMeasured;
	private boolean coordinateSet;
	private String timeStamp;

	private double x, y, z;
	private double instrumentHeight;
	private HashMap<String,String> properties;

	public CgPoint()
	{
		setMeasured(true);
		setCoordinateSet(false);
		setTimeStamp(new Date());
		properties = new HashMap<String,String>();
	}

	public String getCgPoint()
	{
		DecimalFormatSymbols otherSeperators = new DecimalFormatSymbols(Locale.GERMAN);
		otherSeperators.setDecimalSeparator('.');
		otherSeperators.setGroupingSeparator(',');
		DecimalFormat df = new DecimalFormat("0.000000", otherSeperators);

		String cgPoint = "<CgPoints>\n"
				+ "<CgPoint name=\"" + name + "\" oID=\"" + name + "\" ";

		if(this.isMeasured) {
			cgPoint += "role=\"measured\" ";
		} else {
			cgPoint += "role=\"control point\" ";
		}

		cgPoint += "timeStamp=\"" + timeStamp + "\">";

		if(isCoordinateSet() || !isMeasured()) {
			cgPoint += df.format(x) + " " + df.format(y) + " " + df.format(z);
		}

		cgPoint += "</CgPoint>\n";

		if(!this.properties.isEmpty())
		{
			cgPoint += "<Feature name=\"" + name + "\" code=\"pointCode\">\n";

			Set<String> labels = properties.keySet();
			for(String label : labels) {
				cgPoint += "<Property label=\"" + label + "\" value=\"" + properties.get(label) + "\"/>\n";
			}

			cgPoint += "</Feature>\n";
		}

		cgPoint += "</CgPoints>";

		return cgPoint;
	}

	public void addProperty(String label, String value) {
		properties.put(label, value);
	}

	public double getXValue() {
		return x;
	}

	public void setXValue(double x) {
		this.x = x;
		setCoordinateSet(true);
	}

	public double getYValue() {
		return y;
	}

	public void setYValue(double y) {
		this.y = y;
		setCoordinateSet(true);
	}

	public double getZValue() {
		return z;
	}

	public void setZValue(double z) {
		this.z = z;
		setCoordinateSet(true);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isMeasured() {
		return isMeasured;
	}

	public void setMeasured(boolean isMeasured) {
		this.isMeasured = isMeasured;
	}

	public boolean isCoordinateSet() {
		return coordinateSet;
	}

	public void setCoordinateSet(boolean coordinateSet) {
		this.coordinateSet = coordinateSet;
	}

	public String getTimeStamp() {
		return timeStamp;
	}

	public void setTimeStamp(String timeStamp) {
		this.timeStamp = timeStamp;
	}

	public void setTimeStamp(Date timeStamp)
	{
		SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss");
		this.timeStamp = date.format(timeStamp) + "T" + time.format(timeStamp);
	}

	public double getInstrumentHeight() {
		return instrumentHeight;
	}

	public void setInstrumentHeight(double instrumentHeight) {
		this.instrumentHeight = instrumentHeight;
	}
}
